package com.junbao.message.infrastructure.queue;


/**
 * @ClassName Task
 * @Description 要运行的任务
 * @date 2017-03-20
 * @version 1.0.0
 *
 */
public abstract class AbstractTask implements Runnable {
	/**任务id. 如果是分布式部署多台应用，那次id要是全局唯一的*/
	private final String id;
	/**当前时间为基点，多少之后执行*/
	private int secondsLater;
	/**第几圈*/
	private Integer cycleNum;
	/**任务属性*/
	private TaskAttribute taskAttribute;

	/**
	 * 
	 * @param id 任务id
	 */
	public AbstractTask(String id) {
		super();
		this.id = id;
	}
	
	/**
	 * 
	 * @param id 任务id
	 */
	public AbstractTask(String id,int secondsLater) {
		super();
		this.id = id;
		this.secondsLater = secondsLater;
	}
	
	/**
	 * 
	 * @param id 任务id
	 */
	public AbstractTask(String id,int secondsLater,TaskAttribute taskAttribute) {
		super();
		this.id = id;
		this.secondsLater = secondsLater;
		this.taskAttribute = taskAttribute;
	}

	/**
	 * 	倒计数，为0时即可执行此任务
	 */
    public void countDown() {
        this.cycleNum--;
    }

	public String getId() {
		return id;
	}

	public Integer getCycleNum() {
		return cycleNum;
	}


	public void setCycleNum(Integer cycleNum) {
		this.cycleNum = cycleNum;
	}

	public int getSecondsLater() {
		return secondsLater;
	}
	
	public TaskAttribute getTaskAttribute() {
		return taskAttribute;
	}

	public void setTaskAttribute(TaskAttribute taskAttribute) {
		this.taskAttribute = taskAttribute;
	}

	public void setSecondsLater(int secondsLater) {
		this.secondsLater = secondsLater;
	}

	/**
	 * 任务是否可执行
	 * @return
	 */
	public boolean taskAlreadyExecute(){
		return System.currentTimeMillis() >= taskAttribute.getExecuteTimeStamp();
	}
	
	@Override
	public String toString() {
		return "Task [id=" + id + ", cycleNum=" + cycleNum + "]";
	}

}
